<?php namespace Admin\Controller; 
//商品表控制器

//分类表
use Common\Model\Cate;
//品牌表
use Common\Model\Brand;
//商品表
use Common\Model\Goods;
//类型属性
use Common\Model\TypeAttr;
//货品列表
use Common\Model\Detail;
//商品属性表
use Common\Model\GoodsAttr;
class GoodsController extends CommonController{
    private $GoodsModel;
//  自定义构造函数
    public function __auto(){
        //引Model文件
        $this->GoodsModel = new \Common\Model\Goods;
    } 
    
//  首页
    public function index(){
    	$goodsData = $this->GoodsModel->get();
		if(!$goodsData) View::error('你还没有添加商品，请先添加商品',U('add'));
//		p($goodsData);
		View::with('goodsData',$goodsData);
        View::make();
    }
    
//  添加
    public function add(){
    	
		if(IS_POST){
//			p(Q("post."));
//			exit;
			$goodsModel = new Goods();
			if(!$goodsModel->store()) View::error($goodsModel->getError());
			View::success("添加成功",U('index'));
		}
		
//		2.处理所属品牌
		$brandModel = new Brand();
		$brandData = $brandModel->getAll();
		View::with('brandData',$brandData);
		
//		1.处理所属分类
		$cateModel = new Cate();
		$cateData = $cateModel->getAll();
//		p($cateData);
		View::with('cateData',$cateData);

        View::make();
    }
	
	/**
	 * 获得类型属性
	 */
	public function getAttr(){
//		得到类型id
		$tid = Q('post.t',0,'intvala');
		//通过类型id找到类型属性
		$typeAttrModel = new TypeAttr;
		$TypeAttrData = $typeAttrModel->where("type_tid={$tid}")->get();
		//p($TypeAttrData);//--要到Network里查看--
		foreach($TypeAttrData as $k =>$v){
			//把字符串变为数组,将来好循环
			$TypeAttrData[$k]['tavalue'] = explode(',', $v['tavalue']);
		}
//		把数据转化为json格式
		echo json_encode($TypeAttrData);
		exit;
	}
	
	/**
	 * 异步删除图片
	 */
	 public function reImg(){
	     $url = Q('post.url');
//		 echo $url;
		$Img = pathinfo($url);
		p($Img);
		$Img_thumb = str_replace('.'.$Img['extension'],'_thumb.'.$Img['extension'] , $Img);
		p($Img_thumb);
		unlink($Img);
		unlink($Img_thumb);
	 }
	 
	 
	
//  编辑
    public function edit(){
    	$goodsModel = new Goods();
    	if(IS_POST){
    		if(!$goodsModel->edit()) View::error($goodsModel->getError());
			View::success("修改成功",U('index'));
    	}
//  	1.获得旧数据
		$aid = Q("get.aid",0,'intval');
		
		$goodsData = $goodsModel
					->join("shop_detail",'aid','=','goods_aid')
					->where("goods_aid={$aid}")
					->find();
//		4.商品图册
		$goodsData['images']=explode(',', $goodsData['images']);		
//		p($goodsData);
		View::with('goodsData',$goodsData);
		
		//2.处理所属分类
        $cateModel = new Cate();
        $cateData = $cateModel->getAll();
        View::with('cateData',$cateData);
		
//		3.处理所属品牌
		$brandModel = new Brand();
		$brandData = $brandModel->getAll();
//		p($brandData);
		View::with('brandData',$brandData);

//		5.处理商品属性和规格
		$goodsAttrModel = new GoodsAttr();
		$goodsAttrData = $goodsAttrModel
							->join('type_attr','type_attr_taid','=','taid')
							->where("goods_aid={$aid}")
							->get();
		
//		p($goodsAttrData);
//		获得taid就是为了以后排除已经选过的类型	属性
		$taid =[];
		foreach ($goodsAttrData as $k=>$v) {
			$goodsAttrData[$k]['tavalue'] = explode(',', $v['tavalue']);
			$taid[] = $v['taid'];
		}
//		p($taid);
//		把商品没有选过来的属性也拿过来
		$typeAttrModel = new TypeAttr();
		//把数组元素组合为字符串
		$taidStr = implode(',', $taid);
//		p($taidStr);
		$typeAttr = $typeAttrModel
					->where("taid NOT IN ({$taidStr}) AND type_tid={$goodsData['type_tid']}")
					->get();
		
//		p($typeAttr);
		foreach ($typeAttr as $v) {
			$goodsAttrData[] =array(
				'gtvalue'=>'',
				'taname'=>$v['taname'],
				'tavalue'=>explode(';', $v['taname']),
				'class'=>$v['class']
			);
		}
//		p($goodsAttrData);
		View::with('goodsAttrData',$goodsAttrData);
    	View::make();
    }
	
//  删除(子集内容)
    public function del(){
        $aid = Q("get.aid",0,'intval');
		$GoodsModel = $this->GoodsModel->where("aid={$aid}")->DELETE();
//		p($shoplistData);
		View::success("删除成功",U('index'));
    }
	
	//uploadify异步上传 (列表图)
	public function upload(){
	    $file = Upload::path('Upload/Content/' . date('Y/m/d'))->make();
	    if (empty($file)) {
	        // 相当于：echo json_encode(Upload::getError());exit;
	        $this->ajax(Upload::getError());
	    } else {
	    	
//			ext:后缀名
	    	$thumbImg = str_replace(".{$file[0]['ext']}", "_thumb.{$file[0]['ext']}",$file[0]['path']);
			$img = Image::thumb($file[0]['path'],$thumbImg,200,200,5);
		
			
	        /** $file内部就是以下这个数组
	            $file = array(
	                0 => array(
		                'path' => 'Upload/Content/15/8/123981239172.jpg'    ,
		                'url' => 'http://localhost/cms_edu/Upload/Content/15/8/123981239172.jpg',
		                'image' => 1
	            ),
	        );**/
	        //上传成功，把上传好的信息返给js 也就是uploadify
	        $data = $file[0];
	        // 相当于：echo json_encode($data);exit;
	        $this->ajax($data);
	    }
	}
	
	
	//uploadify异步上传  (商品图册)
	public function uploadifys(){
	    $file = Upload::path('Upload/Content/' . date('Y/m/d'))->make();
	    if (empty($file)) {
	        // 相当于：echo json_encode(Upload::getError());exit;
	        $this->ajax(Upload::getError());
	    } else {
	    	
//			ext:后缀名 小图
	    	$thumbImg = str_replace(".{$file[0]['ext']}", "_thumb.{$file[0]['ext']}",$file[0]['path']);
			$img = Image::thumb($file[0]['path'],$thumbImg,54,54,5);
//			大图
			$bigImg = str_replace(".{$file[0]['ext']}", "_big.{$file[0]['ext']}",$file[0]['path']);
			$big = Image::thumb($file[0]['path'],$bigImg,800,800,5);
//			中图
			$midImg = str_replace(".{$file[0]['ext']}", "_mid.{$file[0]['ext']}",$file[0]['path']);
			$mid = Image::thumb($file[0]['path'],$midImg,320,320,5);
			
	        /** $file内部就是以下这个数组
	            $file = array(
	                0 => array(
		                'path' => 'Upload/Content/15/8/123981239172.jpg'    ,
		                'url' => 'http://localhost/cms_edu/Upload/Content/15/8/123981239172.jpg',
		                'image' => 1
	            ),
	        );**/
	        //上传成功，把上传好的信息返给js 也就是uploadify
	        $data = $file[0];
	        // 相当于：echo json_encode($data);exit;
	        $this->ajax($data);
	    }
	}
	
}